<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 用户列表 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <!-- 删选表单 start -->
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除 </el-button>
                <!-- 性别 -->
                <el-select v-model="userVo.user.gender" placeholder="性别" class="handle-select mr10">
                    <el-option key="0" label="全部" value=""></el-option>
                    <el-option key="1" label="男" value="男"></el-option>
                    <el-option key="2" label="女" value="女"></el-option>
                </el-select>
                <!-- 账户 -->
                <el-input v-model="userVo.user.account" placeholder="账户" class="handle-input mr10"></el-input>
                <!-- 生日 -->
                <el-date-picker
                    v-model="birthday"
                    type="daterange"
                    unlink-panels
                    range-separator="至"
                    start-placeholder="生日：开始日期"
                    end-placeholder="结束日期"
                    value-format="yyyy-MM-dd"
                >
                </el-date-picker>
                <!-- 注册时间 -->
                <el-date-picker
                    v-model="regTime"
                    type="daterange"
                    align="right"
                    unlink-panels
                    range-separator="至"
                    start-placeholder="注册：开始日期"
                    end-placeholder="结束日期"
                    :picker-options="pickerOptions"
                    value-format="yyyy-MM-dd HH:mm:ss"
                >
                </el-date-picker>

                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <!-- 删选表单 end -->

            <!-- 表格数据 start -->
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="account" label="账户"></el-table-column>
                <el-table-column prop="nick" label="昵称"></el-table-column>

                <el-table-column label="头像(查看大图)" align="center">
                    <template slot-scope="scope">
                        <el-image
                            class="table-td-thumb"
                            style="border-radius: 50%"
                            :src="scope.row.avr"
                            :preview-src-list="[scope.row.avr]"
                        >
                        </el-image>
                    </template>
                </el-table-column>

                <el-table-column prop="gender" label="性别"></el-table-column>
                <el-table-column prop="authPw" label="是否陪玩">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.authPw === 'y'" type="success">已认证</el-tag>
                        <el-tag v-else-if="scope.row.authPw === 'n'" type="danger">未认证</el-tag>
                        <el-tag v-else type="warning">待审核</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="birthday" label="生日"></el-table-column>
                <el-table-column prop="regTime" label="注册时间"></el-table-column>

                <el-table-column label="状态" align="center">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.status === 'y'" type="success">正常</el-tag>
                        <el-tag v-else type="danger">锁定</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="账户余额">
                    <template slot-scope="scope">￥{{ scope.row.money }}.00</template>
                </el-table-column>

                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-lx-refresh" @click="handleStatus(scope.$index, scope.row, 'y')"
                            >恢复
                        </el-button>
                        <el-button type="text" icon="el-icon-delete" class="red" @click="handleStatus(scope.$index, scope.row, 'n')"
                            >锁定</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
            <!-- 表格数据 end -->

            <!-- 分页区 start -->
            <!-- <div class="pagination">
                <el-pagination background layout="total, prev, pager, next" :current-page="userVo.pageNum"
                    :page-size="pageSize" :total="pageTotal" @current-change="handlePageChange"></el-pagination>
            </div> -->
            <!-- 分页区 -->
            <div class="block">
                <el-pagination
                    @current-change="handleCurrentChange"
                    :current-page.sync="userVo.pageNum"
                    :page-size="3"
                    layout="prev, pager, next, jumper"
                    :page-count="totalPages"
                >
                </el-pagination>
            </div>
            <!-- 分页区 end -->
        </div>
    </div>
</template>

<script>
export default {
    name: 'basetable',
    data() {
        return {
            userVo: {
                user: {
                    gender: '',
                    account: '',
                    startBirthday: '',
                    endBirthday: '',
                    startRegTime: '',
                    endRegTime: ''
                },
                pageNum: 1
            },
            totalPages: 0,
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            form: {},
            idx: -1,
            id: -1,

            pickerOptions: {
                shortcuts: [
                    {
                        text: '最近一周',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', [start, end]);
                        }
                    },
                    {
                        text: '最近一个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                            picker.$emit('pick', [start, end]);
                        }
                    },
                    {
                        text: '最近三个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                            picker.$emit('pick', [start, end]);
                        }
                    }
                ]
            },
            birthday: [],
            regTime: []
        };
    },

    created() {
        this.getData();
    },

    methods: {
        // 获取 userList 数据
        getData() {
            this.$axios.post('/admin/user/list', this.userVo).then((r) => {
                this.tableData = r.data.data.data;
                console.log(r);
                this.totalPages = r.data.data.totalPages;
                console.log(this.totalPages);
            });
        },
        handleSearch() {
            if (this.birthday != null) {
                this.userVo.user.startBirthday = this.birthday[0];
                this.userVo.user.endBirthday = this.birthday[1];
            }
            if (this.birthday != null) {
                this.userVo.user.startRegTime = this.regTime[0];
                this.userVo.user.endRegTime = this.regTime[1];
            }
            this.getData();
        },
        // 锁定或解锁操作，管理员仅可作此二者操作，权力不能太大吧
        handleStatus(index, row, status) {
            if (row.status == status) {
                if (row.status == 'n') {
                    this.$message.warning('该用户已被锁定，且勿重复操作');
                } else {
                    this.$message.warning('该用户状态正常，且勿重复操作');
                }
                return;
            }
            // 二次确认删除
            let confMsg = status == 'n' ? '确定要锁定用户' : '确定要解锁用户';
            this.$confirm(confMsg + '  " ' + row.nick + ' "  吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$axios.get('/admin/user/updateStatus', { params: { id: row.id, status: status } }).then((r) => {
                        if (r.data.code == 200) {
                            this.$message.success(status == 'n' ? '锁定成功' : '解锁成功');
                            row.status = status;
                        } else {
                            this.$message.warning('操作失败，请稍候再试');
                        }
                    });
                })
                .catch(() => {});
        },

        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = row;
            this.editVisible = true;
        },

        // 分页导航
        handleCurrentChange(pageNum) {
            this.userVo.pageNum = pageNum;
            this.getData();
            console.log(`当前页: ${pageNum}`);
        }
        // // 分页导航
        // handlePageChange(val) {
        //     this.$set(this.userVo, 'pageIndex', val);
        //     this.pageNum
        //     this.getData();
        // }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}

.table {
    width: 100%;
    font-size: 14px;
}

.red {
    color: #ff0000;
}

.mr10 {
    margin-right: 10px;
}

.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
